﻿using Fishing.Abstractions;
using Fishing.Common;
using Fishing.View;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Input;

namespace Fishing.Converter
{
    class KeyGestureKeyValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is KeyGesture keyGesture)
            {
                var key = keyGesture.Key;

                return Array.IndexOf(Enum.GetValues<AllowedKeys>(), (AllowedKeys)key);
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var index = int.Parse((string)parameter);

            var settingsService = App.Services.GetRequiredService<ISettingsService>();

            if (index < 6)
            {
                var oldKeyGesture = settingsService.Settings.QuickUpdateKeyBinding;
                var newKeyGesture = new KeyGesture(MapToKeyFromAllowedKeys((int)value), oldKeyGesture.Modifiers);

                if (settingsService.Settings.TrySetKeyBingding(KeyBindingMap.QuickUpdate, newKeyGesture))
                {
                    return newKeyGesture;
                }

                var snackbar = App.Services.GetRequiredService<IViewProvider>().GetView<SettingsView>().SnackbarThree;

                snackbar.MessageQueue?.Enqueue("快捷键冲突", null, null, null, false, true, TimeSpan.FromSeconds(1));

                return oldKeyGesture;
            }
            else if (index < 9)
            {
                var oldKeyGesture = settingsService.Settings.ForceQuitKeyBinding;
                var newKeyGesture = new KeyGesture(MapToKeyFromAllowedKeys((int)value), oldKeyGesture.Modifiers);

                if (settingsService.Settings.TrySetKeyBingding(KeyBindingMap.ForceQuit, newKeyGesture))
                {
                    return newKeyGesture;
                }

                var snackbar = App.Services.GetRequiredService<IViewProvider>().GetView<SettingsView>().SnackbarThree;

                snackbar.MessageQueue?.Enqueue("快捷键冲突", null, null, null, false, true, TimeSpan.FromSeconds(1));

                return oldKeyGesture;
            }
            else
            {
                var oldKeyGesture = settingsService.Settings.ExhaleKeyBinding;
                var newKeyGesture = new KeyGesture(MapToKeyFromAllowedKeys((int)value), oldKeyGesture.Modifiers);

                if (settingsService.Settings.TrySetKeyBingding(KeyBindingMap.Exhale, newKeyGesture))
                {
                    return newKeyGesture;
                }

                var snackbar = App.Services.GetRequiredService<IViewProvider>().GetView<SettingsView>().SnackbarThree;

                snackbar.MessageQueue?.Enqueue("快捷键冲突", null, null, null, false, true, TimeSpan.FromSeconds(1));

                return oldKeyGesture;
            }
        }

        private static Key MapToKeyFromAllowedKeys(int index)
        {
            return (Key)Enum.GetValues<AllowedKeys>()[index];
        }
    }
}